#include<iostream>
#include<string>
using namespace std;

class String{

private:
    char* data;
    
public:
    String(){
        data =new char[1];
        data[0]='\0';

    }
    
    String(const char* str){
        if(str==NULL){
            data = new char[1];
            data[0]='\0';
            
        }
        else{
            data =new char[strlen(str)+1];
            strcpy(data, str);
            
        }
    }
    bool is_substring(const String & sub_str){
        
        char* p1 = this->data;
        char* p2 =sub_str.data;
        int cnt;
        while(*p1!='\0'){
            cnt =0;
            while(*(p1+cnt) != '\0' && *(p2+cnt)!='0'){
                if(*(p1+cnt)!=*(p2+cnt))break;
                cnt++;
            }
            if('\0'==*(p2+cnt))
                return true;
            p1++;
        }
        return false;
    }
    
        int find_str (const char* sub_str ){
        char* p1= this ->data;
        const char* p2 =sub_str;
        int cnt;
        while(*p1!='\0'){
            cnt =0;
            while(*(p1+cnt)!='\0' && *(p2+cnt)!='\0'){
                if(*(p1+cnt)!= *(p2+cnt))break;
                cnt++;
            }
            if('\0'==*(p2+cnt))
                return p1-this->data;
            p1++;
        }
        return -1;
    }
        
    char* replace1(int pos, int len , const char* str){
            char* newdata= NULL;
            if(len<strlen(str))
                newdata = new char [strlen(data)+(strlen(str)-len)];
            
            else 
                newdata = data;
            strncpy(newdata,data,pos);
            strcpy(newdata+pos, str);
            strcpy(newdata+pos+strlen(str), data+pos+len);
            if(len<strlen(str))
                    delete []data;
            data=newdata;
            return data;
            
            
        }
        int replace(const char* find ,  const char* replace){
            int pos = find_str(find );
            int count =0;
            while(pos!=-1){
                replace1(pos, strlen(find), replace);
                count++;
                pos= find_str(find);
                
                
            }
            return count;
        }
    void display(){
        cout<< data <<"  ";
    }
    
};
    
int main(){
        String str1=String("abc");
        String str2=String("abcd");
        str1.display();
        cout<<"is substring of ";
        str2.display();
        cout<<":";
        cout<<str2.is_substring(str1)<<endl;
        cout<<"find cdd from ";
        str2.display();
        cout<<":";
        cout<<str2.find_str("cdd")<<endl;
        
        cout<<"replace ";
        str2.display();
        cout<<" 1,2,e=";
        cout<<str2.replace1(1, 2, "e");
        cout<<endl;
        cout<<"replace ";
        str1.display();cout<<"a ee =";
        str1.replace("a", "ee");
        str1.display();
        
    }
